package com.zjxu.fair.common;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.zjxu.fair.R;
import com.zjxu.fair.community.CommunityFragment;
import com.zjxu.fair.home.HomeFragment;
import com.zjxu.fair.message.MessageFragment;
import com.zjxu.fair.personal.PersonalFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager mViewPager;
    private ImageView ivHome, ivCommunity, ivMessage, ivPersonal;
    private TextView tvHome, tvCommunity, tvMessage, tvPersonal;
    //并非点击图案才有响应，点击到附近的块也是有响应的，所以要为这个块设置监听，这里要声明
    private LinearLayout llHome, llCommunity, llMessage, llPersonal;

    private FragmentVPAdapter mVPAdapter;
    private List<Fragment> mFragmentList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        mVPAdapter = new FragmentVPAdapter(getSupportFragmentManager(), mFragmentList);
        mViewPager.setAdapter(mVPAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                /*Toast.makeText(MainActivity.this, "页面"+position, Toast.LENGTH_SHORT).show();*/
                onviewPagerSelected(position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //默认进入选中首页
        onviewPagerSelected(0);

        llHome.setOnClickListener(this);
        llCommunity.setOnClickListener(this);
        llMessage.setOnClickListener(this);
        llPersonal.setOnClickListener(this);
    }

    private void onviewPagerSelected(int position) {

        resetBottomState();

        switch (position) {
            case 0:
                ivHome.setSelected(true);
                tvHome.setTextColor(getResources().getColor(R.color.black));
                break;
            case 1:
                ivCommunity.setSelected(true);
                tvCommunity.setTextColor(getResources().getColor(R.color.black));
                break;
            case 2:
                ivMessage.setSelected(true);
                tvMessage.setTextColor(getResources().getColor(R.color.black));
                break;
            case 3:
                ivPersonal.setSelected(true);
                tvPersonal.setTextColor(getResources().getColor(R.color.black));
                break;
            default:
                break;
        }

    }

    private void resetBottomState() {
        ivHome.setSelected(false);
        tvHome.setTextColor(getResources().getColor(R.color.black));
        ivCommunity.setSelected(false);
        tvCommunity.setTextColor(getResources().getColor(R.color.black));
        ivMessage.setSelected(false);
        tvMessage.setTextColor(getResources().getColor(R.color.black));
        ivPersonal.setSelected(false);
        tvPersonal.setTextColor(getResources().getColor(R.color.black));
    }

    private void initData() {
        mFragmentList = new ArrayList<>();

//        TestNavFragment fragHome = TestNavFragment.newInstance("首页", "");
//        TestNavFragment fragCommunity = TestNavFragment.newInstance("社区", "");
//        TestNavFragment fragMessage = TestNavFragment.newInstance("消息", "");
//        TestNavFragment fragPersonal = TestNavFragment.newInstance("个人", "");

        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new CommunityFragment());
        mFragmentList.add(new MessageFragment());
        mFragmentList.add(new PersonalFragment());
    }

    private void initView() {
        mViewPager = findViewById(R.id.vp);
        llHome = findViewById(R.id.ll_home);
        llCommunity = findViewById(R.id.ll_community);
        llMessage = findViewById(R.id.ll_message);
        llPersonal = findViewById(R.id.ll_personal);

        ivHome = findViewById(R.id.iv_home);
        ivCommunity = findViewById(R.id.iv_community);
        ivMessage = findViewById(R.id.iv_messgae);
        ivPersonal = findViewById(R.id.iv_personal);

        tvHome = findViewById(R.id.tv_home);
        tvCommunity = findViewById(R.id.tv_community);
        tvMessage = findViewById(R.id.tv_message);
        tvPersonal = findViewById(R.id.tv_personal);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.ll_home:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.ll_community:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.ll_message:
                mViewPager.setCurrentItem(2);
                break;
            case R.id.ll_personal:
                mViewPager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }
}